<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>ajax下载文件进度条</title>
		<style type="text/css">
			.process-box{
				width: 80%;
				height: 5px;
				border-radius: 5px;
				border: 1px solid green;
				margin: 0 auto;
				margin-top: 8%;
				
			}
			#processRate{
				width: 0%;
				background: #008000;
				height: 100%;
			}
		</style>
	</head>
	<body>
		<button onclick="download()">下载</button>
		<div class="process-box">
			<div id="processRate">
				
			</div>
		</div>
		<div style="text-align: center;"><h2 id="rateValue">0%</h2></div>
		
	</body>
</html>


<script type="text/javascript">
	var rateDom = document.getElementById("processRate");
	function download(){
		var xhr = new XMLHttpRequest();
		xhr.open("GET","http://101.35.187.112/lifeifei/jdk-8u131-linux-x64.tar.gz",true);
		xhr.addEventListener("progress",function(event){
			let loaded = event.loaded;
			let total = event.total;
			let rate = ((loaded / total) * 100).toFixed(2);
			console.log(rate)
			rateDom.style.width = rate+"%";
			document.getElementById("rateValue").innerText=rate+"%";
		},false);
		
		xhr.send();
	}
</script>
